﻿Public Class PlaylistOrganize
  Public Sub New()
    Me.InitializeComponent()

    Me.WP7ButtonUp.Text = Language.GetLanguageString("Up")
    Me.WP7ButtonDown.Text = Language.GetLanguageString("Down")
    Me.WP7ButtonRemove.Text = Language.GetLanguageString("Remove")
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemMenu.Text = Language.GetLanguageString("Menu")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
    Me.MenuItemSelectAll.Text = Language.GetLanguageString("SelectAll")
    Me.MenuItemSelectNone.Text = Language.GetLanguageString("SelectNone")
    Me.MenuItemInvertSelection.Text = Language.GetLanguageString("InvertSelection")

    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public Sub SetColors(ByVal bg As Color, ByVal fg As Color, ByVal ifg As Color)
    Me.BackColor = bg
    Me.ForeColor = fg
    Me.WP7ButtonDown.BackColor = bg
    Me.WP7ButtonDown.ForeColor = fg
    Me.WP7ButtonDown.InactiveForeColor = ifg
    Me.WP7ButtonUp.BackColor = bg
    Me.WP7ButtonUp.ForeColor = fg
    Me.WP7ButtonUp.InactiveForeColor = ifg
    Me.WP7ButtonRemove.BackColor = bg
    Me.WP7ButtonRemove.ForeColor = fg
    Me.WP7ButtonRemove.InactiveForeColor = ifg
    Me.ListViewPlaylist.BackColor = bg
    Me.ListViewPlaylist.ForeColor = fg
    For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
      lvi.BackColor = bg
      lvi.ForeColor = fg
    Next
  End Sub

  Public Sub SetListItemFromAVPL(ByVal avpl As Playlists.AVPLPlaylist)
    Try
      Me.ListViewPlaylist.Items.Clear()
      If avpl IsNot Nothing Then
        System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
        For Each avple As Playlists.AVPLPlaylistEntry In avpl
          Dim lvi As New ListViewItem

          lvi.BackColor = Me.BackColor
          lvi.ForeColor = Me.ForeColor

          If avple.Text.Length > 0 Then
            If avple.Subtext.Length > 0 Then
              lvi.Text = avple.Text & " / " & avple.Subtext
            Else
              lvi.Text = avple.Text
            End If
          Else
            lvi.Text = avple.Subtext
          End If

          lvi.SubItems.Add(avple.GetHashCode)

          Me.ListViewPlaylist.Items.Add(lvi)
        Next
      End If
    Finally
      System.Windows.Forms.Cursor.Current = Cursors.Default
    End Try
  End Sub

  Public ReadOnly Property ListItemHashes() As List(Of Integer)
    Get
      ListItemHashes = New List(Of Integer)
      For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
        ListItemHashes.Add(Integer.Parse(lvi.SubItems(1).Text))
      Next
    End Get
  End Property

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub WP7ButtonUp_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonUp.MouseUp
    Dim pos As New List(Of Integer)
    Dim top As Integer = 0

    For i As Integer = 0 To Me.ListViewPlaylist.Items.Count - 1
      Dim lvi As ListViewItem = Me.ListViewPlaylist.Items(i)
      If lvi.Checked Then
        If lvi.Index > top Then
          pos.Add(lvi.Index)
        Else
          top += 1
        End If
      End If
    Next

    For Each i As Integer In pos
      Dim lvi As ListViewItem = Me.ListViewPlaylist.Items(i)
      Me.ListViewPlaylist.Items.RemoveAt(i)
      Me.ListViewPlaylist.Items.Insert(i - 1, lvi)
    Next
  End Sub

  Private Sub WP7ButtonDown_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonDown.MouseUp
    Dim pos As New List(Of Integer)
    Dim bottom As Integer = Me.ListViewPlaylist.Items.Count - 1

    For i As Integer = Me.ListViewPlaylist.Items.Count - 1 To 0 Step -1
      Dim lvi As ListViewItem = Me.ListViewPlaylist.Items(i)
      If lvi.Checked Then
        If lvi.Index < bottom Then
          pos.Add(lvi.Index)
        Else
          bottom -= 1
        End If
      End If
    Next

    For Each i As Integer In pos
      Dim lvi As ListViewItem = Me.ListViewPlaylist.Items(i)
      Me.ListViewPlaylist.Items.RemoveAt(i)
      Me.ListViewPlaylist.Items.Insert(i + 1, lvi)
    Next
  End Sub

  Private Sub WP7ButtonRemove_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonRemove.MouseUp
    Dim removeList As New List(Of ListViewItem)

    For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
      If lvi.Checked Then
        removeList.Add(lvi)
      End If
    Next

    For Each lvi As ListViewItem In removeList
      Me.ListViewPlaylist.Items.Remove(lvi)
    Next
  End Sub

  Public Sub ListViewPlaylist_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles ListViewPlaylist.ItemCheck
    Dim val As Boolean = False

    If e.NewValue Then
      val = True
    Else
      For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
        If lvi.Index <> e.Index AndAlso lvi.Checked Then
          val = True
          Exit For
        End If
      Next
    End If

    Me.WP7ButtonDown.Enabled = val
    Me.WP7ButtonUp.Enabled = val
    Me.WP7ButtonRemove.Enabled = val
  End Sub

  Private Sub MenuItemSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSelectAll.Click
    For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
      lvi.Checked = True
    Next
  End Sub

  Private Sub MenuItemSelectNone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSelectNone.Click
    For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
      lvi.Checked = False
    Next
  End Sub

  Private Sub MenuItemInvertSelection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemInvertSelection.Click
    For Each lvi As ListViewItem In Me.ListViewPlaylist.Items
      lvi.Checked = Not lvi.Checked
    Next
  End Sub

  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub
End Class